package com.powerrich.controller;

import com.powerrich.pojo.VipPO;
import com.powerrich.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 黄雨
 * @title: VipController
 * @projectName demo
 * @description: TODO
 * @date 2019-04-0413:52
 */
@RestController
@RequestMapping("vip")
public class VipController {

    @Autowired
    private VipService vipService;

    @Autowired
    private RedisTemplate<String ,Object> redisTemplate;

    @GetMapping("getVipById/{id}")
    @ResponseBody
    public VipPO getGirlById(@PathVariable(value="id")Integer id){
        VipPO vipPO = new VipPO();
        HashOperations<String, String, VipPO> opsForHash = redisTemplate.opsForHash();
        System.out.println(opsForHash.entries("VipPO"));
        if(opsForHash.hasKey("VipPO",id.toString())){
            vipPO =opsForHash.get("VipPO",id.toString());

        }else {
            vipPO = vipService.getVipById(id);
        }
        return vipPO;
    }

    @GetMapping("getAllVip")
    @ResponseBody
    public List<VipPO> getAllVip(){
        return vipService.getAllVip();
    }

    @PostMapping("addVip")
    @ResponseBody
    public void addVip(@RequestBody VipPO vipPO){
        vipService.addVip(vipPO);
    }

    @PostMapping("updateVip")
    @ResponseBody
    public void updateVip(@RequestBody VipPO vipPO){
        vipService.updateVip(vipPO);
    }

    @DeleteMapping("deleteVipById/{id}")
    @ResponseBody
    public void deleteVipById(@PathVariable(value="id")Integer id){
        vipService.deleteVipById(id);
    }


}
